﻿namespace Dcms.OleDbDAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.OleDb;

    public class Dcms_DownDAL : IDcms_DownDAL
    {
        public Dcms_DownModel AddModel(Dcms_DownModel model)
        {
            string sQLString = "insert into Dcms_Down(Down_CateID,Down_FileType,Down_FileName,Down_FileSize,Down_Image,Down_LocalPath,Down_OtherPath,Down_Countion,Down_State,Down_CertTime,Down_ExFlag1,Down_ExFlag2,Down_ExFlag3,Down_ExFlag4,Down_ExFlag5,Down_ExFlag6,Down_ExFlag7,Down_ExFlag8,Down_ExFlag9,Down_ExFlag10) values(@Down_CateID,@Down_FileType,@Down_FileName,@Down_FileSize,@Down_Image,@Down_LocalPath,@Down_OtherPath,@Down_Countion,@Down_State,@Down_CertTime,@Down_ExFlag1,@Down_ExFlag2,@Down_ExFlag3,@Down_ExFlag4,@Down_ExFlag5,@Down_ExFlag6,@Down_ExFlag7,@Down_ExFlag8,@Down_ExFlag9,@Down_ExFlag10)";
            OleDbParameter[] cmdParms = new OleDbParameter[] { 
                new OleDbParameter("@Down_CateID", OleDbType.Integer), new OleDbParameter("@Down_FileType", OleDbType.VarChar), new OleDbParameter("@Down_FileName", OleDbType.VarChar), new OleDbParameter("@Down_FileSize", OleDbType.VarChar), new OleDbParameter("@Down_Image", OleDbType.VarChar), new OleDbParameter("@Down_LocalPath", OleDbType.VarChar), new OleDbParameter("@Down_OtherPath", OleDbType.VarChar), new OleDbParameter("@Down_Countion", OleDbType.LongVarChar), new OleDbParameter("@Down_State", OleDbType.VarChar), new OleDbParameter("@Down_CertTime", OleDbType.DBTimeStamp), new OleDbParameter("@Down_ExFlag1", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag2", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag3", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag4", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag5", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag6", OleDbType.VarChar), 
                new OleDbParameter("@Down_ExFlag7", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag8", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag9", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag10", OleDbType.VarChar)
             };
            cmdParms[0].Value = model.Down_CateID;
            cmdParms[1].Value = model.Down_FileType;
            cmdParms[2].Value = model.Down_FileName;
            cmdParms[3].Value = model.Down_FileSize;
            cmdParms[4].Value = model.Down_Image;
            cmdParms[5].Value = model.Down_LocalPath;
            cmdParms[6].Value = model.Down_OtherPath;
            cmdParms[7].Value = model.Down_Countion;
            cmdParms[8].Value = model.Down_State;
            cmdParms[9].Value = model.Down_CertTime;
            cmdParms[10].Value = model.Down_ExFlag1;
            cmdParms[11].Value = model.Down_ExFlag2;
            cmdParms[12].Value = model.Down_ExFlag3;
            cmdParms[13].Value = model.Down_ExFlag4;
            cmdParms[14].Value = model.Down_ExFlag5;
            cmdParms[15].Value = model.Down_ExFlag6;
            cmdParms[0x10].Value = model.Down_ExFlag7;
            cmdParms[0x11].Value = model.Down_ExFlag8;
            cmdParms[0x12].Value = model.Down_ExFlag9;
            cmdParms[0x13].Value = model.Down_ExFlag10;
            model.Down_ID = OleDbHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public bool CopyDown(int SourceCateID, int DestCateID)
        {
            string sQLString = "insert into Dcms_Down (Down_CateID, Down_FileType, Down_FileName, Down_FileSize, Down_Image, Down_LocalPath, Down_OtherPath, Down_Countion, Down_State, Down_CertTime, Down_ExFlag1, Down_ExFlag2, Down_ExFlag3, Down_ExFlag4, Down_ExFlag5, Down_ExFlag6, Down_ExFlag7, Down_ExFlag8, Down_ExFlag9, Down_ExFlag10)";
            sQLString = sQLString + "select @DestCateID, Down_FileType, Down_FileName, Down_FileSize, Down_Image, Down_LocalPath, Down_OtherPath, Down_Countion, Down_State, Down_CertTime, Down_ExFlag1, Down_ExFlag2, Down_ExFlag3, Down_ExFlag4, Down_ExFlag5, Down_ExFlag6, Down_ExFlag7, Down_ExFlag8, Down_ExFlag9, Down_ExFlag10 from Dcms_Down where Down_CateID=@SourceCateID";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@SourceCateID", OleDbType.Integer), new OleDbParameter("@DestCateID", OleDbType.Integer) };
            cmdParms[0].Value = SourceCateID;
            cmdParms[1].Value = DestCateID;
            return OleDbHelper.ExecuteSql(sQLString, cmdParms) > 0;
        }

        public IList<Dcms_DownModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_DownModel> list = new List<Dcms_DownModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_DownModel item = new Dcms_DownModel();
                if (ds.Tables[0].Rows[i]["Down_ID"].ToString() != "") item.Down_ID = int.Parse(ds.Tables[0].Rows[i]["Down_ID"].ToString());
                if (ds.Tables[0].Rows[i]["Down_CateID"].ToString() != "") item.Down_CateID = int.Parse(ds.Tables[0].Rows[i]["Down_CateID"].ToString());
                item.Down_FileType = ds.Tables[0].Rows[i]["Down_FileType"].ToString();
                item.Down_FileName = ds.Tables[0].Rows[i]["Down_FileName"].ToString();
                item.Down_FileSize = ds.Tables[0].Rows[i]["Down_FileSize"].ToString();
                item.Down_Image = ds.Tables[0].Rows[i]["Down_Image"].ToString();
                item.Down_LocalPath = ds.Tables[0].Rows[i]["Down_LocalPath"].ToString();
                item.Down_OtherPath = ds.Tables[0].Rows[i]["Down_OtherPath"].ToString();
                item.Down_Countion = ds.Tables[0].Rows[i]["Down_Countion"].ToString();
                item.Down_State = ds.Tables[0].Rows[i]["Down_State"].ToString();
                if (ds.Tables[0].Rows[i]["Down_CertTime"].ToString() != "") item.Down_CertTime = DateTime.Parse(ds.Tables[0].Rows[i]["Down_CertTime"].ToString());
                item.Down_ExFlag1 = ds.Tables[0].Rows[i]["Down_ExFlag1"].ToString();
                item.Down_ExFlag2 = ds.Tables[0].Rows[i]["Down_ExFlag2"].ToString();
                item.Down_ExFlag3 = ds.Tables[0].Rows[i]["Down_ExFlag3"].ToString();
                item.Down_ExFlag4 = ds.Tables[0].Rows[i]["Down_ExFlag4"].ToString();
                item.Down_ExFlag5 = ds.Tables[0].Rows[i]["Down_ExFlag5"].ToString();
                item.Down_ExFlag6 = ds.Tables[0].Rows[i]["Down_ExFlag6"].ToString();
                item.Down_ExFlag7 = ds.Tables[0].Rows[i]["Down_ExFlag7"].ToString();
                item.Down_ExFlag8 = ds.Tables[0].Rows[i]["Down_ExFlag8"].ToString();
                item.Down_ExFlag9 = ds.Tables[0].Rows[i]["Down_ExFlag9"].ToString();
                item.Down_ExFlag10 = ds.Tables[0].Rows[i]["Down_ExFlag10"].ToString();
                list.Add(item);
            }
            return list;
        }

        public bool DeleteAllCheck(string AllID)
        {
            return OleDbHelper.ExecuteSql("delete from Dcms_Down where Down_ID In (" + AllID + ")", null) > 0;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_Down where Down_ID=@Down_ID";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Down_ID", OleDbType.Integer) };
            cmdParms[0].Value = ID;
            OleDbHelper.ExecuteSql(sQLString, cmdParms);
        }

        public string GetDataCount(string SqlStr)
        {
            return OleDbHelper.GetNoteCount(SqlStr).ToString();
        }

        public IList<Dcms_DownModel> GetDataListByPage(string SqlStr, int CurPage, int PageSize)
        {
            return this.DataSetToList(OleDbHelper.CreateDataSet(SqlStr, (CurPage - 1) * PageSize, PageSize));
        }

        public Dcms_DownModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_Down where Down_ID=@Down_ID";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Down_ID", OleDbType.Integer) };
            cmdParms[0].Value = ID;
            Dcms_DownModel model = new Dcms_DownModel();
            DataSet ds = OleDbHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_DownModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelList()
        {
            string sql = "select * from Dcms_Down";
            return OleDbHelper.CreateDataSet(sql);
        }

        public Dcms_DownModel GetNewModelByCatID(int CatID)
        {
            string sQLString = " select top 1 * from Dcms_Down where Down_CateID=@Down_CateID order by Down_ID desc ";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Down_CateID", OleDbType.Integer) };
            cmdParms[0].Value = CatID;
            Dcms_DownModel model = new Dcms_DownModel();
            DataSet ds = OleDbHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_DownModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public bool MoveDown(int SourceCateID, int DestCateID)
        {
            string sQLString = "update Dcms_Down set Down_CateID=@DestCateID where Down_CateID=@SourceCateID";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@SourceCateID", OleDbType.Integer), new OleDbParameter("@DestCateID", OleDbType.Integer) };
            cmdParms[0].Value = SourceCateID;
            cmdParms[1].Value = DestCateID;
            return OleDbHelper.ExecuteSql(sQLString, cmdParms) > 0;
        }

        public Dcms_DownModel Update(Dcms_DownModel model)
        {
            string sQLString = "update Dcms_Down set Down_CateID=@Down_CateID,Down_FileType=@Down_FileType,Down_FileName=@Down_FileName,Down_FileSize=@Down_FileSize,Down_Image=@Down_Image,Down_LocalPath=@Down_LocalPath,Down_OtherPath=@Down_OtherPath,Down_Countion=@Down_Countion,Down_State=@Down_State,Down_CertTime=@Down_CertTime,Down_ExFlag1=@Down_ExFlag1,Down_ExFlag2=@Down_ExFlag2,Down_ExFlag3=@Down_ExFlag3,Down_ExFlag4=@Down_ExFlag4,Down_ExFlag5=@Down_ExFlag5,Down_ExFlag6=@Down_ExFlag6,Down_ExFlag7=@Down_ExFlag7,Down_ExFlag8=@Down_ExFlag8,Down_ExFlag9=@Down_ExFlag9,Down_ExFlag10=@Down_ExFlag10 where Down_ID=@Down_ID";
            OleDbParameter[] cmdParms = new OleDbParameter[] { 
                new OleDbParameter("@Down_CateID", OleDbType.Integer), new OleDbParameter("@Down_FileType", OleDbType.VarChar), new OleDbParameter("@Down_FileName", OleDbType.VarChar), new OleDbParameter("@Down_FileSize", OleDbType.VarChar), new OleDbParameter("@Down_Image", OleDbType.VarChar), new OleDbParameter("@Down_LocalPath", OleDbType.VarChar), new OleDbParameter("@Down_OtherPath", OleDbType.VarChar), new OleDbParameter("@Down_Countion", OleDbType.LongVarChar), new OleDbParameter("@Down_State", OleDbType.VarChar), new OleDbParameter("@Down_CertTime", OleDbType.DBTimeStamp), new OleDbParameter("@Down_ExFlag1", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag2", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag3", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag4", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag5", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag6", OleDbType.VarChar), 
                new OleDbParameter("@Down_ExFlag7", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag8", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag9", OleDbType.VarChar), new OleDbParameter("@Down_ExFlag10", OleDbType.VarChar), new OleDbParameter("@Down_ID", OleDbType.Integer)
             };
            cmdParms[0].Value = model.Down_CateID;
            cmdParms[1].Value = model.Down_FileType;
            cmdParms[2].Value = model.Down_FileName;
            cmdParms[3].Value = model.Down_FileSize;
            cmdParms[4].Value = model.Down_Image;
            cmdParms[5].Value = model.Down_LocalPath;
            cmdParms[6].Value = model.Down_OtherPath;
            cmdParms[7].Value = model.Down_Countion;
            cmdParms[8].Value = model.Down_State;
            cmdParms[9].Value = model.Down_CertTime;
            cmdParms[10].Value = model.Down_ExFlag1;
            cmdParms[11].Value = model.Down_ExFlag2;
            cmdParms[12].Value = model.Down_ExFlag3;
            cmdParms[13].Value = model.Down_ExFlag4;
            cmdParms[14].Value = model.Down_ExFlag5;
            cmdParms[15].Value = model.Down_ExFlag6;
            cmdParms[0x10].Value = model.Down_ExFlag7;
            cmdParms[0x11].Value = model.Down_ExFlag8;
            cmdParms[0x12].Value = model.Down_ExFlag9;
            cmdParms[0x13].Value = model.Down_ExFlag10;
            cmdParms[20].Value = model.Down_ID;
            OleDbHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public bool UpdateState(string AllID, int StateValue)
        {
            return OleDbHelper.ExecuteSql("Update Dcms_Down Set Down_State='" + StateValue.ToString() + "' where Down_ID In (" + AllID + ")", null) > 0;
        }
    }
}

